//
//  MKScrollView.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/5/5.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "MKScrollView.h"

@implementation MKScrollView

- (id)initWithPhoto:(UIImage *)image Frame:(CGRect)frame Center:(CGPoint)center{
    
    if((self = [super init])){
        
        self.frame = frame;
        self.myFrame = frame;
        self.myImage = image;
        
        //PhotoImageView
        self.parkPortImageView = [[MKTapDetectImageView alloc] initWithFrame:CGRectZero];
        self.parkPortImageView.tapDelegate = self;
        self.parkPortImageView.contentMode = UIViewContentModeCenter;
        self.parkPortImageView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.parkPortImageView];
        
        //SetUp
        self.backgroundColor = [UIColor whiteColor];
        self.delegate = self;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.decelerationRate = UIScrollViewDecelerationRateFast;
        
        [self displayImageWithCenter:center];
    }
    
    return self;
}

- (void)displayImageWithCenter:(CGPoint)centerPoint{
    
    if(self.parkPortImageView.image == nil){
        
        self.maximumZoomScale = 2;
        self.minimumZoomScale = 1;
        self.zoomScale = 1;
        self.contentSize = CGSizeMake(0, 0);
        
        if(self.myImage){
            
            //Setup Image
            self.parkPortImageView.image = self.myImage;
            self.parkPortImageView.hidden = NO;
            
            //Setup Photo Frame
            CGRect parkPortImageViewFrame;
//            parkPortImageViewFrame.origin = CGPointZero;
            parkPortImageViewFrame.size = self.myImage.size;
            self.parkPortImageView.frame = parkPortImageViewFrame;
            self.contentSize = parkPortImageViewFrame.size;
            
//            [self scrollRectToVisible:CGRectMake(centerPoint.x, centerPoint.y, 1, 1) animated:NO];
            
            self.minimumZoomScale = 1.0 * self.myFrame.size.width / self.myImage.size.width ;
            
            // Set zoom to minimum zoom
            [self setMaxMinZoomScalesForCurrentBounds];
        }else{
            
            self.parkPortImageView.hidden = YES;
        }
        
        [self setNeedsLayout];
    }
}

- (void)setMaxMinZoomScalesForCurrentBounds{
    
    //Reset
    self.maximumZoomScale = 1;
    self.minimumZoomScale = 1;
    self.zoomScale = 1;
    
    if(self.parkPortImageView == nil){
        
        return;
    }
    
    CGSize boundSize = self.myFrame.size;
    CGSize imageSize = self.parkPortImageView.frame.size;
    
    //Calculate Min
    CGFloat xScale = boundSize.width / imageSize.width;   // the scale needed to perfectly fit the image width-wise
    CGFloat yScale = boundSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise
    CGFloat minScale = MIN(xScale, yScale);               // use minimum of these to allow the image to become fully visible

    // If image is smaller than the screen then ensure we show it at
    // min scale of 1
    if(xScale > 1 && yScale > 1){
        
        minScale = 1.0;
    }
    
    //Calculate Max
    CGFloat maxScale = 2.0; // Allow double scale
    // on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the
    // maximum zoom scale to 0.5.
    if([UIScreen instancesRespondToSelector:@selector(scale)]){
        
        maxScale = maxScale / [[UIScreen mainScreen] scale];
    }
    
    //Set
    self.maximumZoomScale = maxScale;
    self.minimumZoomScale = minScale;
    self.zoomScale = minScale;
    
    // Reset position
    self.parkPortImageView.frame = CGRectMake(0, 0, self.parkPortImageView.frame.size.width, self.parkPortImageView.frame.size.height);
    [self setNeedsLayout];
}

#pragma mark - Layout
- (void)layoutSubviews{
    
    //Super
    [super layoutSubviews];
    
    // Center the image as it becomes smaller than the size of the screen
    CGSize boundSize = self.myFrame.size;
    CGRect frameToCenter = self.parkPortImageView.frame;
    
    //Horizontally
    if(frameToCenter.size.width < boundSize.width){
        
        frameToCenter.origin.x = floorf((boundSize.width - frameToCenter.size.width) / 2.0);
    }else{
        
        frameToCenter.origin.x = 0;
    }
    
    //Vertically
    if(frameToCenter.size.height < boundSize.height){
        
        frameToCenter.origin.y = floorf((boundSize.height - frameToCenter.size.height) / 2.0);
    }else{
        
        frameToCenter.origin.y = 0;
    }
    
    //Center
    if(!CGRectEqualToRect(self.parkPortImageView.frame, frameToCenter)){
        
        self.parkPortImageView.frame = frameToCenter;
    }
}

#pragma mark - UIScrollView Delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return self.parkPortImageView;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
    
    NSLog(@"MKScrollView ZoomIng ");
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
}

#pragma mark - MKTapDetectImageViewDelegate
- (void)handleSingleTap:(CGPoint)touchPoint{
    
    if (self.mkDelegate != nil && [self.mkDelegate conformsToProtocol:@protocol(MKScrollViewDelegate)])
    {
        [self.mkDelegate singleTapDetected:[NSNumber numberWithFloat:touchPoint.x] Y:[NSNumber numberWithFloat:touchPoint.y]];
    }
}

- (void)handleDoubleTap:(CGPoint)touchPoint{
    
    //Zoom Scale
    if(self.zoomScale == self.maximumZoomScale){
        
        //Zoom Out
        [self setZoomScale:self.minimumZoomScale animated:YES];
    }else{
        
        //Zoom In
        [self zoomToRect:CGRectMake(touchPoint.x, touchPoint.y, 1, 1) animated:YES];
    }
}

- (void)handleTripleTap:(CGPoint)touchPoint{
    
    
}

//ImageView
- (void)imageView:(UIImageView *)imageView singleTapDetected:(UITouch *)touch{
    
    [self handleSingleTap:[touch locationInView:imageView]];
}

- (void)imageView:(UIImageView *)imageView doubleTapDetected:(UITouch *)touch{
    
    [self handleDoubleTap:[touch locationInView:imageView]];
}

- (void)imageView:(UIImageView *)imageView tripleTapDetected:(UITouch *)touch{
    
    
}

@end
